<template>
  <div class="collapse-box">
    <div class="collapse-item" v-for="(item, index) in items" :key="item.id">
      <div class="line"></div>

      <div class="collapse-item-title" @click="handleToggle(item.id)">
        <span
          class="collapse-item-title-text"
          :class="{
            'text-active': activeId === item.id,
            'text-unactive': activeId !== item.id,
          }"
          >{{ item.title }}</span
        >
        <template v-if="activeId === item.id">
          <el-icon class="text-active"><ArrowDownBold /></el-icon>
        </template>
        <template v-else>
          <el-icon class="text-unactive"><ArrowRightBold /></el-icon>
        </template>
      </div>

      <div
        class="line"
        v-show="item.id === activeId || index === items.length - 1"
      ></div>

      <transition name="collapse">
        <div class="collapse-item-sub-title" v-show="activeId === item.id">
          <div
            class="collapse-item-sub-title-item"
            v-for="subItem in activeSubtitle"
            :key="subItem"
          >
            {{ subItem }}
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import type { CollapseItem, CollapseProps } from "./types";
defineOptions({
  name: "Collapse",
});

const props = defineProps<CollapseProps>();

const activeId = ref("");
const activeSubtitle = computed(() => {
  if (activeId.value) {
    const temp = props.items.find(
      (item: CollapseItem) => item.id === activeId.value
    )?.subTitle;
    return temp;
  }
  return [];
});

const handleToggle = (id: string) => {
  if (!id) return;
  if (activeId.value === id) {
    activeId.value = "";
  } else {
    activeId.value = id;
  }
};
</script>

<style lang="scss" scoped>
.collapse-box {
  opacity: 1;
  color: #fff;
  padding-top: 10px;
  padding-bottom: 10px;
}

.collapse-item-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.3s ease;
  height: 25px;
  padding-left: 20px;
  padding-right: 20px;
  &:hover {
    background: #22304a; // 悬浮时背景
  }
}

.collapse-item-title-text {
  font-size: 12px;
  font-weight: 600;
}

.text-active {
  color: $text-active;
}

.text-unactive {
  color: $text-secondary;
}

.line {
  height: 1px;
  background: linear-gradient(
    to right,
    rgba(255, 255, 255, 0),
    $primary-color 50%,
    rgba(255, 255, 255, 0)
  );
}

.collapse-item-sub-title {
  display: flex;
  justify-content: center;
  flex-direction: column;
  overflow: hidden;
  padding-left: 20px;
  padding-right: 20px;
}

.collapse-item-sub-title-item {
  display: flex;
  align-items: center;
  padding: 3px 0;
  color: $text-secondary;
  font-size: 12px;
  position: relative;
  border-bottom: none; // 取消原有 border

  &::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 1px;
    background: linear-gradient(
      to right,
      rgba($primary-color, 0),
      rgba($primary-color, 0.35) 50%,
      rgba($primary-color, 0)
    );
    pointer-events: none;
  }

  &:last-child {
    border-bottom: none;
  }
}

/* 添加展开收起动画 */
.collapse-enter-active,
.collapse-leave-active {
  transition: all 0.3s ease;
  max-height: 300px;
}

.collapse-enter-from,
.collapse-leave-to {
  max-height: 0;
  opacity: 0;
  transform: translateY(-10px);
}
</style>
